import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React from 'react';
import { Spin } from 'antd';

var PromiseRender = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(PromiseRender, _React$PureComponent);

  var _super = _createSuper(PromiseRender);

  function PromiseRender() {
    var _this;

    _classCallCheck(this, PromiseRender);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      component: null
    };

    _this.checkIsInstantiation = function (target) {
      if (!React.isValidElement(target)) {
        return target;
      }

      return function () {
        return target;
      };
    };

    return _this;
  }

  _createClass(PromiseRender, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setRenderComponent(this.props);
    } //  eslint-disable-next-line

  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      // new Props enter
      this.setRenderComponent(nextProps);
    } // set render Component : ok or error

  }, {
    key: "setRenderComponent",
    value: function setRenderComponent(props) {
      var _this2 = this;

      var ok = this.checkIsInstantiation(props.ok);
      var error = this.checkIsInstantiation(props.error);
      props.promise.then(function () {
        _this2.setState({
          component: ok
        });
      }).catch(function () {
        _this2.setState({
          component: error
        });
      });
    } // Determine whether the incoming component has been instantiated
    // AuthorizedRoute is already instantiated
    // Authorized  render is already instantiated, children is no instantiated
    // Secured is not instantiated

  }, {
    key: "render",
    value: function render() {
      var Component = this.state.component;
      return Component ? /*#__PURE__*/React.createElement(Component, this.props) : /*#__PURE__*/React.createElement("div", {
        style: {
          width: '100%',
          height: '100%',
          margin: 'auto',
          paddingTop: 50,
          textAlign: 'center'
        }
      }, /*#__PURE__*/React.createElement(Spin, {
        size: "large"
      }));
    }
  }]);

  return PromiseRender;
}(React.PureComponent);

export { PromiseRender as default };